<?php session_start();

    $con = mysql_connect("localhost","root","");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }

    mysql_select_db("progin_171_13509008", $con);

        
            global $contentCount;
            global $pageNumber;

            $pageNumber=$_GET["pageNumber"];
            function displayContent($row,$result){

                global $pageNumber;
                $number = 6;
                for ($i=0; $i < $number*$pageNumber; $i++)
                {  mysql_fetch_array($result);}
                
                for ($i = 0; $i < $number; $i++){
                    $row= mysql_fetch_array($result);
                    if($row['content_type']== 'link'){
                        echo "<h1>".$row['title']."</h1>";
                        echo "<h2>".$row['content_filled']."</h2>";
                        echo "<h3>".$row['content_description']."</h3>";
                        echo "<h3>Posted by: <a href='detail.php'>".$row['user_name']."</a></h3>";
                    }
                    if($row['content_type']== 'image'){
                        echo "<h1>".$row['title']."</h1>";
                        echo "<img src='".$row['content_filled']."' height=500px; ></img>";
                        echo "<h3>Posted by: <a href='detail.php'>".$row['user_name']."</a></h3>";
                    }
                    if($row['content_type']== 'video'){
                        echo "<h1>".$row['title']."</h1>";
                        echo $row['content_filled'];
                        echo "<h3>Posted by: <a href='detail.php'>".$row['user_name']."</a></h3>";
                    }
                }
            }
                
                if(!isset($_POST['sort'])){
                   $sort = "new" ;
                }else{
                    $sort = $_POST['sort'];
                }
                if(!isset($_POST['filter'])){
                    $filter = "";
                }else{
                    $filter = $_POST['filter'];
                }
                if(!isset($_GET['tagname'])){
                    $getlink = "";
                }else{
                    $getlink = $_GET['tagname'];
                }
                $words = explode(",", $filter);
                if($getlink == ""){
                    if($words[0] == ""){
                        if($sort == "popular"){
                            $query = "Select user_name, content.title, content.content_type, content.content_filled, content.posting_time from
                                        (Select c.id_content as e from ((Select b.id_content from (select id_content,count(*) as result from like_content where status = 1 group by id_content order by result desc) as b)
                                        union distinct
                                        (select not_like_result.id_content as not_like_result_id_content from (select id_content from like_content where status = 0 or status = 2 )as not_like_result)) as c) as most_like_order_id_content
                                        , content
                                where most_like_order_id_content.e = id_content";
                        }else{
                            if($sort == "comment"){
                                $query = "Select user_name, content.title, content.content_type, content.content_filled, content.posting_time from content inner join (select most_comment.id_content as id_most_commented from (select id_content, count(*) as result from comment group by id_content order by result Desc) as most_comment)  comment_result
                                        on content.id_content = comment_result.id_most_commented";
                            }else{
                                $query = "select distinct user_name, content.title, content.content_type, content.content_filled from 
                                        (select id_content, TIMESTAMPDIFF(SECOND, posting_time, now()) as time from content order by time) time_order, content
                                        order by time_order.time";
                            }
                        }
                        $result = mysql_query($query);
                        $row = mysql_num_rows($result);
                        if($row == 0){
                            echo "No tag exists in database";
                        }else{
                            displayContent($row,$result);
                            while ($countRow = mysql_fetch_array($result)){
                                $contentCount++;
                            }
                        }
                    }else{
                        for($i = 0 ; $i < count($words) ; $i++){
                            $query = "CREATE VIEW upil AS SELECT * FROM content WHERE id_content = (SELECT id_content FROM tag_content WHERE id_tag =
                            (SELECT id_tag FROM tag WHERE tag_name = '".$words[$i]."'))";
                            $result = mysql_query($query);
                            if($sort == "popular"){
                                $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from
                                        (Select c.id_content as e from ((Select b.id_content from (select id_content,count(*) as result from like_content where status = 1 group by id_content order by result desc) as b)
                                        union distinct
                                        (select not_like_result.id_content as not_like_result_id_content from (select id_content from like_content where status = 0 or status = 2 )as not_like_result)) as c) as most_like_order_id_content
                                        , upil
                                where most_like_order_id_content.e = id_content";
                            }else{
                                if($sort == "comment"){
                                    $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from upil inner join (select most_comment.id_content as id_most_commented from (select id_content, count(*) as result from comment group by id_content order by result Desc) as most_comment)  comment_result
                                        on upil.id_content = comment_result.id_most_commented";
                                }else{
                                    $query = "select distinct upil.user_name, upil.title, upil.content_type, upil.content_filled from 
                                        (select id_content, TIMESTAMPDIFF(SECOND, posting_time, now()) as time from upil order by time) time_order, upil 
                                        order by time_order.time";
                                }
                            }
                            $result = mysql_query($query);
                            $row = mysql_num_rows($result);
                            $query2 = "drop VIEW upil";
                            $result2 = mysql_query($query2);
                            if($row == 0){
                                if($words[$i] == "" || $words[$i] == " "){

                                }else{
                                    echo "No tag exists in database";
                                }
                            }else{
                                displayContent($row,$result);
                                while ($countRow = mysql_fetch_array($result)){
                                    $contentCount++;
                                }
                            }
                        }
                    }
                }else{
                
                    //link tag
                    $query = "CREATE VIEW upil AS SELECT * FROM content WHERE id_content = (SELECT id_content FROM tag_content WHERE id_tag =
                    (SELECT id_tag FROM tag WHERE tag_name = '".$getlink."'))";
                    $result = mysql_query($query);
                    if($sort == "popular"){
                        $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from
                                (Select c.id_content as e from ((Select b.id_content from (select id_content,count(*) as result from like_content where status = 1 group by id_content order by result desc) as b)
                                union distinct
                                (select not_like_result.id_content as not_like_result_id_content from (select id_content from like_content where status = 0 or status = 2 )as not_like_result)) as c) as most_like_order_id_content
                                , upil
                        where most_like_order_id_content.e = id_content";
                    }else{
                        if($sort == "comment"){
                            $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from upil inner join (select most_comment.id_content as id_most_commented from (select id_content, count(*) as result from comment group by id_content order by result Desc) as most_comment)  comment_result
                                on upil.id_content = comment_result.id_most_commented";
                        }else{
                            $query = "select distinct upil.user_name, upil.title, upil.content_type, upil.content_filled from 
                                (select id_content, TIMESTAMPDIFF(SECOND, posting_time, now()) as time from upil order by time) time_order, upil 
                                order by time_order.time";
                        }
                    }
                    $result = mysql_query($query);
                    $row = mysql_num_rows($result);
                    displayContent($row,$result);

                // count page
                while ($countRow = mysql_fetch_array($result)){
                    $contentCount++;
                }
                $query2 = "drop VIEW upil";
                $result2 = mysql_query($query2);
                }
                global $totalPage;
                $totalPage = ceil($contentCount/6);
                while ($totalPage >0) {
                    echo "<button type='button' onclick='changePage(".$totalPage.")' style='float: right;'>".$totalPage."</button>";
                    $totalPage--;
                }
                mysql_close();
                /*
                * To change this template, choose Tools | Templates
                * and open the template in the editor.
                */
            ?>


